Skip to content

NOISSUE - Improve CoAP message handling#108

Merged
dborovcanin merged 2 commits intoabsmach:mainfrom
felixgateru:noissue-coap
Sep 16, 2025
Merged

NOISSUE - Improve CoAP message handling#108
dborovcanin merged 2 commits intoabsmach:mainfrom
felixgateru:noissue-coap

Conversation

@felixgateru
Copy link
Contributor

Pull request title should be MF-XXX - description or NOISSUE - description where XXX is ID of issue that this PR relate to.
Please review the CONTRIBUTING.md file for detailed contributing guidelines.

What does this do?

This pr improves coap message handling for the coap proxy

Which issue(s) does this PR fix/relate to?

Put here Resolves #XXX to auto-close the issue that your PR fixes (if such)

List any changes that modify/break current functionality

The pr introduces responses for failure on handling coap messages to the client

Have you included tests for your changes?

No

Did you document any new/modified functionality?

Notes

Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
pkg/coap/coap.go Outdated
return
default:
n, clientAddr, err := l.ReadFromUDP(buffer)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it is not logically any different, let's keep listen logic under default.

pkg/coap/coap.go Outdated
return
}

p.mutex.Lock()
Copy link
Contributor

@dborovcanin dborovcanin Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's encapsulate this mutex in p.newConn. Something like (I didn't test this one, there may be errors):

func (p *Proxy) newConn(clientAddr *net.UDPAddr) (*Conn, error) {
	p.mutex.Lock()
	defer p.mutex.Unlock() // Simpler locking handling, also safer access since we are locking not
	//  only the map, but also conn.ServeConn and conn.ClientAddr.
	conn, ok := p.connMap[clientAddr.String()]
	if !ok {
		conn = &Conn{
			clientAddr: clientAddr,
		}
		addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(p.config.TargetHost, p.config.TargetPort))
		if err != nil {
			return nil, err
		}
		t, err := net.DialUDP("udp", nil, addr)
		if err != nil {
			return nil, err
		}
		conn.serverConn = t
		p.connMap[clientAddr.String()] = conn
	}
	return conn, nil
}

Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
@dborovcanin dborovcanin merged commit 503150b into absmach:main Sep 16, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants